package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.FundRecei;


public class FundReceiManager extends HibernateDaoSupport {

	public void addFundRecei(FundRecei fundRecei) {
		
		getHibernateTemplate().save(fundRecei);
	}
	
	public void updateFundRecei(FundRecei fundRecei) {
		
		getHibernateTemplate().update(fundRecei);
	}
	
	public void deleteFundRecei(int id){
		
		getHibernateTemplate().delete(getFundRecei(id));
	}
	
	public FundRecei getFundRecei(int id){
		FundRecei d =(FundRecei)getHibernateTemplate().get(FundRecei.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<FundRecei> listFundRecei() {
		return getHibernateTemplate().find("from FundRecei");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String type) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(new Date());
		List <FundRecei>list = getHibernateTemplate().find("from FundRecei f where f.type = '" + type +"' and f.date = '" + d + "'" );
		FundRecei sp = new FundRecei();
		for(FundRecei s : list){
			sp.setDbank(sp.getDbank() + s.getDbank());
			sp.setDcash(sp.getDcash() + s.getDcash());
			sp.setRbank(sp.getRbank() + s.getRbank());
			sp.setRcash(sp.getRcash() + s.getRcash());
		}
		list.add(sp);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <FundRecei>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,FundRecei fundRecei) {
		
		List <FundRecei>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from FundRecei f where f.type = '" + fundRecei.getType() + "'";
		if(!fundRecei.getCompany().equals("")){
			sql += "and f.company = '" + fundRecei.getCompany() + "'";
		}
		if(!fundRecei.getHandler().equals("")){
			
			sql += "and f.handler = '" + fundRecei.getHandler() + "'";
		}
		if(fundRecei.getDate() != null){
			String d = sdf.format(fundRecei.getDate());
			
			sql += "and  f.date = '" + d + "'";
		}
		
		list = getHibernateTemplate().find(sql);
		FundRecei sp = new FundRecei();
		for(FundRecei s : list){
			sp.setDbank(sp.getDbank() + s.getDbank());
			sp.setDcash(sp.getDcash() + s.getDcash());
			sp.setRbank(sp.getRbank() + s.getRbank());
			sp.setRcash(sp.getRcash() + s.getRcash());
		}
		list.add(sp);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <FundRecei>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	


}



